/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package th.manager;

import com.google.inject.Inject;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author fikri
 */
public class PoskodManager {
    
    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    
        public String getPoskodAjaxAutoComplete(String kod, String poskod) {      
        StringBuilder sb = new StringBuilder("[");

        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select p.poskod from Poskod p " +
                "where p.negeri.code = :kod and poskod LIKE :poskod");
        query.setString("kod", kod); 
        query.setString("poskod", poskod + "%");         
        return Arrays.toString(query.list().toArray()).replace("[", "").replace("]", "");       
    }
    
    public String getPoskodAndDescAjaxAutoComplete(String kod, String poskod) {      
        StringBuilder sb = new StringBuilder("[");

        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select p.poskod from Poskod p " +
                "where p.negeri.code = :kod and poskod LIKE :poskod");
        Query query1 = session.createQuery("select p.nama from Poskod p " +
                "where p.negeri.code = :kod and poskod LIKE :poskod");
        query.setString("kod", kod); 
        query.setString("poskod", poskod + "%");
        query1.setString("kod",kod);
        query1.setString("poskod", poskod + "%");
        List l1=query.list();
        List l2=query1.list();
        String[] str=new String[l1.size()];
        for(int i=0;i<l1.size();i++)
        {
            str[i]=l1.get(i) + "-" +l2.get(i);
        }
       
        return Arrays.toString(str).replace("[","").replace("]","");
        
    }
    
    //added in 17/6/2013
    public String getPoskodAjax(String kod) {      
       StringBuilder sb = new StringBuilder("[");

        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select p.poskod from Poskod p " +
                "where p.negeri.code = :kod");
        Query query1 = session.createQuery("select p.nama from Poskod p " +
                "where p.negeri.code = :kod");
        query.setString("kod", kod); 
        query1.setString("kod",kod);
        List l1=query.list();
        List l2=query1.list();
        String[] str=new String[l1.size()];
        for(int i=0;i<l1.size();i++)
        {
            str[i]=l1.get(i) + "-" +l2.get(i);
        }
       
        return Arrays.toString(str).replace("[","").replace("]","");
    }
}
